import java.io.IOException;

import javax.xml.bind.annotation.XmlElementDecl.GLOBAL;

import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GLUConstants;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;

import pkg_entity.Sprite;

public class main {
	State state;
	
	float x = 0, y = 0;
	float rotation = 180;
	
	int fps;
	
	long lastFPS;
	long lastFrame;

	Texture testtexture;
	
	private TextureLoader textureLoader;
	
	public static void main(String[] args) throws LWJGLException, IOException {
		main main = new main();
		main.start();
		
	}
	
	public enum State {
		INTRO, GAME, MAIN_MENU;
	}
	
	public void start() throws LWJGLException, IOException{
		state = State.INTRO;
		Display.setDisplayMode(new DisplayMode(800,600));
		initGame();
	}
	
	private void initGame() throws LWJGLException, IOException {
		Display.create();
		
		
		
		initGL();
		init();
		getDelta();
		lastFPS = getTime();
		
		while (!Display.isCloseRequested()) {
			int delta = getDelta();
		
			update(delta);
			renderGL();
			
			Display.update();
			Display.sync(60);
		}
		
	}
	
	private void init() throws IOException {
		textureLoader = new TextureLoader();
		testtexture = textureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/knight.png"));
	}

	private void initGL() {
		GL11.glEnable(GL11.GL_TEXTURE_2D);
		GL11.glEnable(GL11.GL_BLEND);
		GL11.glDisable(GL11.GL_DEPTH_TEST);
		
		GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		
		GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
		GL11.glViewport(0, 0, 800, 600);
		
		GL11.glMatrixMode(GL11.GL_PROJECTION);
		GL11.glLoadIdentity();
		GL11.glOrtho(0, 800, 0, 600, 1, -1);
		GL11.glMatrixMode(GL11.GL_MODELVIEW);
		
	}

	private void renderGL() {
		GL11.glBindTexture(GL11.GL_TEXTURE_2D, testtexture.getTextureID());
		
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
		//GL11.glColor3f(1.0f, 0.0f, 0.0f);
		// Was macht das hier alles... TESTEN PLS
		GL11.glBegin(GL11.GL_QUADS);
			GL11.glTexCoord2f(0,0);
			GL11.glVertex2f(x, y);
			GL11.glTexCoord2f(1,0);
			GL11.glVertex2f(x+testtexture.getTextureWidth(),y);
			GL11.glTexCoord2f(1,1);
			GL11.glVertex2f(x+testtexture.getTextureWidth(), y+testtexture.getTextureHeight());
			GL11.glTexCoord2f(0,1);
			GL11.glVertex2f(x, y+testtexture.getTextureHeight());
		GL11.glEnd();
		//Ende Test
		GL11.glPopMatrix();
		
	}

	public void update(int delta) {
		rotation += 0.15f * delta;
			
		if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) x -= 0.35f * delta;		
		if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) x += 0.35f * delta;
		if (Keyboard.isKeyDown(Keyboard.KEY_UP)) y += 0.35f * delta;
		if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) y -= 0.35f * delta;
		if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE));
		if (Mouse.isButtonDown(0)) System.out.println("FIRE!");
		 

		if (x < 0) x = 0;
		if (x > 800) x = 800;
		if (y < 0) y = 0;
		if (y > 600) y = 600;

		updateFPS();
	}
	
	public int getDelta() {
		long time = getTime();
		int delta = (int) (time-lastFrame);
		lastFrame = time;
		return delta;
	}

	
	public void updateFPS() {
		if (getTime() - lastFPS > 1000) {
			Display.setTitle("FPS:" + fps);
			fps = 0;
			lastFPS += 1000;
		}
		fps++;
	}
	
	public long getTime() {
		return (Sys.getTime() * 1000) / Sys.getTimerResolution();
	}
	
	public Sprite getSprite(String ref) {
		return new Sprite(textureLoader,ref);
	}
}